package cn.stylefeng.guns.modular.business.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.core.RecoverConst;
import cn.stylefeng.guns.modular.business.entity.CardQuota;
import cn.stylefeng.guns.modular.business.mapper.CardQuotaMapper;
import cn.stylefeng.guns.modular.business.model.params.CardQuotaParam;
import cn.stylefeng.guns.modular.business.model.result.CardQuotaResult;
import  cn.stylefeng.guns.modular.business.service.CardQuotaService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 卡面值 服务实现类
 * </p>
 *
 * @author jwj
 * @since 2019-07-12
 */
@Service
public class CardQuotaServiceImpl extends ServiceImpl<CardQuotaMapper, CardQuota> implements CardQuotaService {

    @Override
    public void add(CardQuotaParam param){
        CardQuota entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(CardQuotaParam param){
        CardQuota entity = new CardQuota();
        entity.setCardQuotaId(param.getCardQuotaId());
        entity.setDeleteFlag(RecoverConst.DEL_FLAG_TRUE);
        this.updateById(entity);
    }

    @Override
    public void update(CardQuotaParam param){
        CardQuota oldEntity = getOldEntity(param);
        CardQuota newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public CardQuotaResult findBySpec(CardQuotaParam param){
        return null;
    }

    @Override
    public List<CardQuotaResult> findListBySpec(CardQuotaParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(CardQuotaParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    /**
     * 更新状态
     *
     * @param param
     * @author jwj
     * @Date 2019-07-12
     */
    @Override
    public void status(CardQuotaParam param) {
        CardQuota entity = new CardQuota();
        entity.setCardQuotaId(param.getCardQuotaId());
        entity.setStatus(param.getStatus());
        this.updateById(entity);
    }

    private Serializable getKey(CardQuotaParam param){
        return param.getCardQuotaId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private CardQuota getOldEntity(CardQuotaParam param) {
        return this.getById(getKey(param));
    }

    private CardQuota getEntity(CardQuotaParam param) {
        CardQuota entity = new CardQuota();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
